<?php
/**
 * ClipboardController is an instance of Controller (application.components). 
 *
 * Controller is an instance of CController (system.web).
 * CController manages a set of actions which deal with the corresponding user requests. 
 * Through the actions, CController coordinates the data flow between models and views. 
 *
 * Eduow :  International system for education (http://eduow.com)
 * Copyright 2010, Nikolay Stoitsev 
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.controllers
 * @inheritance   class ClipboardController » Controller » CController » CBaseController » CComponent
 * @implements    
 * @since         Eduow v 0.0.3
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */

class ClipboardController extends Controller
{
	/**
	 * @var string the default layout for the views. 
	 */
	public $layout='main';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			'*'=>array(Group::USER, 'min')
		);
	}

	/**
	 * Saves data passed with AJAX request
	 */
	public function actionSafe()
	{
		$form=$this->loadModel();
		$form->user=Yii::app()->user->id;
		if(isset($_POST['Clipboard']))
		{
			$form->attributes=$_POST['Clipboard'];
			$form->validate();
			$form->save();
		}
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	private function loadModel()
	{
		if($this->_model===null)
		{
				$criteria ='user='.Yii::app()->user->id;
				$this->_model=Clipboard::model()->find($criteria);
			
			if($this->_model===null)
				$this->_model=new Clipboard;
		}
		return $this->_model;
	}
}
